// monitor.cpp : 定义应用程序的入口点。
//

#include <windows.h>
#include "monitor.h"

#include "souistd.h"
#include "SHostWnd.h"
#include "commgr2.h"
#include "res/resource.h"

#define RES_TYPE 0
//#define SYSRES_TYPE 0
// #define RES_TYPE 0   //PE
// #define RES_TYPE 1   //ZIP
// #define RES_TYPE 2   //7z
// #define RES_TYPE 2   //文件
//去掉多项支持，以免代码显得混乱
#if (RES_TYPE==1)
#include "resprovider-zip\zipresprovider-param.h"
#else 
#if (RES_TYPE==2)
#include "resprovider-7zip\zip7resprovider-param.h"
#endif
#endif
#ifdef _DEBUG
#define SYS_NAMED_RESOURCE _T("soui-sys-resourced.dll")
#else
#define SYS_NAMED_RESOURCE _T("soui-sys-resource.dll")
#endif

using namespace SOUI;
class SMainDlg : public SHostWnd
{
public:
	SMainDlg():SHostWnd(_T("LAYOUT:XML_MAINWND")){}
	void OnClose(){SHostWnd::DestroyWindow();}
	void OnMaximize(){SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE);}
	void OnRestore(){SendMessage(WM_SYSCOMMAND, SC_RESTORE);}
	void OnMinimize(){SendMessage(WM_SYSCOMMAND, SC_MINIMIZE);}
	void OnClick(){SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE);}
	void onVisible(EventArgs *pEvt){}
	void OnSize(UINT nType, CSize size){
		SetMsgHandled(FALSE);//窗口
	}
	BOOL OnInitDialog(HWND wndFocus, LPARAM lInitParam){return 0;}
protected:
	//soui消息
	EVENT_MAP_BEGIN()
		EVENT_NAME_COMMAND(L"btn_close", OnClose)
		EVENT_NAME_COMMAND(L"btn_min", OnMinimize)
		EVENT_NAME_COMMAND(L"btn_max", OnMaximize)
		EVENT_NAME_COMMAND(L"btn_restore", OnRestore)
		EVENT_NAME_COMMAND(L"button1", OnClick)
		EVENT_NAME_HANDLER(L"onVisible",SOUI::EVT_CMD, onVisible)
		EVENT_NAME_HANDLER(L"maxWidth",SOUI::EVT_CMD, onVisible)
		
	EVENT_MAP_END()
		
	//HostWnd真实窗口消息处理
	BEGIN_MSG_MAP_EX(SMainDlg)
		MSG_WM_INITDIALOG(OnInitDialog)
		MSG_WM_CLOSE(OnClose)
		MSG_WM_SIZE(OnSize)
		CHAIN_MSG_MAP(SHostWnd)
		REFLECT_NOTIFICATIONS_EX()
	END_MSG_MAP()
};

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	OleInitialize(NULL);

	SComMgr2 ComMgr(_T("imgdecoder-png"));
	CAutoRefPtr<SOUI::IRenderFactory> pRenderFactory;
	SASSERT_FMT(ComMgr.CreateRender_GDI((IObjRef**)& pRenderFactory),_T("load interface [render] failed!"));
	CAutoRefPtr<SOUI::IImgDecoderFactory> pImgDecoderFactory;
	SASSERT_FMT(ComMgr.CreateImgDecoder((IObjRef**)& pImgDecoderFactory),_T("load interface [imgdecoder] failed!"));
	pRenderFactory->SetImgDecoderFactory(pImgDecoderFactory);

	SApplication theApp(pRenderFactory, hInstance);
	HMODULE hModSysResource = LoadLibrary(SYS_NAMED_RESOURCE);
	if (!hModSysResource) return -1;
	CAutoRefPtr<IResProvider> sysResProvider;
	CreateResProvider(RES_PE, (IObjRef * *)& sysResProvider);
	sysResProvider->Init((WPARAM)hModSysResource, 0);
	theApp.LoadSystemNamedResource(sysResProvider);
	FreeLibrary(hModSysResource);

	CAutoRefPtr<IResProvider>   pResProvider;
#ifdef _DEBUG		
	CreateResProvider(RES_FILE, (IObjRef **)& pResProvider);
	SASSERT(pResProvider->Init((LPARAM)_T("uires"), 0));
#else
	CreateResProvider(RES_PE, (IObjRef * *)& pResProvider);
	SASSERT(pResProvider->Init((WPARAM)theApp.GetInstance(), 0));
#endif
	theApp.InitXmlNamedID(namedXmlID, ARRAYSIZE(namedXmlID), TRUE);
	theApp.AddResProvider(pResProvider);

	SMainDlg dlgMain;
	dlgMain.Create(GetActiveWindow());
	dlgMain.SendMessage(WM_INITDIALOG);
	dlgMain.CenterWindow(dlgMain.m_hWnd);
	dlgMain.ShowWindow(SW_SHOWNORMAL);
	theApp.Run(dlgMain.m_hWnd);

	OleUninitialize();
	return (int) 0;
}